//============================================================================
// Name        : main.cpp
// Author      : zerofier
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <X11/Xlib.h>

#include "stbi/stbi.h"
#include "bitmap.h"

#define __STR__(v) #v
#define STR(m) __STR__(m)
#ifdef __PROJ_NAME__
#define PROJ_NAME STR(__PROJ_NAME__)
#else
#define PROJ_NAME "";
#endif

bitmap* g_pBitmap = NULL;

int winproc(const XEvent& event);

int main()
{
	Display* display = XOpenDisplay("");
	int screen = DefaultScreen(display);

	XSetWindowAttributes attributes;
	bzero(&attributes, sizeof(XSetWindowAttributes));
	attributes.event_mask |= ExposureMask | ButtonPressMask | ButtonReleaseMask /*| PointerMotionMask*/;

	Window window = XCreateWindow(
			display,
			DefaultRootWindow(display),
			0, 0,
			800,600,
			0,
			CopyFromParent,
			InputOutput,
			DefaultVisual(display, screen),
			CWBackPixel | CWEventMask,
			&attributes);

	XStoreName(display, window, PROJ_NAME);

	XMapWindow(display, window);
	XRaiseWindow(display, window);

	int x, y, comp;
	stbi_uc* image_data = stbi_load("/home/fit/Downloads/A24031CF-0278-4EF3-9197-35CBB2B517F0.JPG", &x, &y, &comp, 0);

	bitmap _bitmap(x, y, image_data, comp);
	g_pBitmap = &_bitmap;

	//_bitmap.getelementfrom(1,1);

	XImage *image = NULL;

	if (!image)
		image = XCreateImage(display, DefaultVisual(display, screen), 24, ZPixmap, 0,
				(char*)_bitmap.create_difmap(), _bitmap.m_width, _bitmap.m_height, __CHAR_BIT__ * sizeof(color_t), sizeof(color_t) * _bitmap.m_width);

	free(image_data);

	XEvent event;
	bzero(&event, sizeof(event));

	_bitmap.analyze();

	while (true)
	{
		if (XPending(display))
		{
			XNextEvent(display, &event);
			winproc(event);
		}
		else
		{
			GC gc = XCreateGC(display, window, 0, NULL);
			if (image)
				XPutImage(display, window, gc, image, 0, 0, 0, 0, image->width, image->height);
			XFreeGC(display, gc);
		}

		XFlush(display);
	}

	XFree(image);

	XUnmapWindow(display, window);
	XDestroyWindow(display, window);
	XCloseDisplay(display);

	return 0;
}

/**
 * event processor
 */
int winproc(const XEvent& event)
{
	GC gc = NULL;

	switch (event.type)
	{
	case Expose:
		if (event.xexpose.count) break;
		gc = XCreateGC(event.xexpose.display, event.xexpose.window, 0, NULL);
		// TODO:
		// XSetForeground( Display* display, GC gc, unsigned long foreground );
		// XDrawPoint(event.xexpose.display, event.xexpose.window, gc, x, y);

		// XCreateImage
		// XPutImage

		XFreeGC(event.xexpose.display, gc);
		break;
	case ButtonPress:
		printf("ButtonPress button = %d (x = %d, y = %d)\n", event.xbutton.button, event.xbutton.x, event.xbutton.y);
		break;
	case ButtonRelease:
		printf("ButtonRelease button = %d (x = %d, y = %d)\n", event.xbutton.button, event.xbutton.x, event.xbutton.y);
		g_pBitmap->analyze(event.xbutton.x, event.xbutton.y);
		break;
	case MotionNotify:
		printf("MotionNotify (x = %d, y = %d)\n", event.xmotion.x, event.xmotion.y);
		break;
	default:
		printf("event.type = %d\n", event.type);
		break;
	}

	return 0;
}
